********************************************************************************
** 	TITLE:		de2013_gles_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ZA5702_en_v4-0-1                                              **
**	DATE:		October 2022 					                              **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "de2013_gles_contact_networks", replace

* Open the dataset

use "ZA5702_en_v4-0-1.dta", clear

* Generate ID variable

gen id = vlfdn

drop if id == -94 // Drop if "not in sampling frame"


***************************
** RELABELLING DISTRICTS **
***************************

label define district 1 "Flensburg - Schleswig" 2 "Nordfriesland - Dithmarschen Nord" 3 "Steinburg - Dithmarschen Süd" 4 "Rendsburg-Eckernförde" 5 "Kiel" 6 "Plön - Neumünster" 7 "Pinneberg" 8 "Segeberg - Stormarn-Mitte" 9 "Ostholstein - Stormarn-Nord" 10 "Herzogtum Lauenburg - Stormarn-Süd" 11 "Lübeck" 12 "Schwerin - Ludwigslust-Parchim I - Nordwestmecklenburg I" 13 "Ludwigslust-Parchim II - Nordwestmecklenburg II - Landkreis Rostock I" 14 "Rostock - Landkreis Rostock II" 15 "Vorpommern-Rügen - Vorpommern-Greifswald I" 16 "Mecklenburgische Seenplatte I - Vorpommern-Greifswald II" 17 "Mecklenburgische Seenplatte II - Landkreis Rostock III" 18 "Hamburg-Mitte" 19 "Hamburg-Altona" 20 "Hamburg-Eimsbüttel" 21 "Hamburg-Nord" 22 "Hamburg-Wandsbek" 23 "Hamburg-Bergedorf - Harburg" 24 "Aurich - Emden" 25 "Unterems" 26 "Friesland - Wilhelmshaven - Wittmund" 27 "Oldenburg - Ammerland" 28 "Delmenhorst - Wesermarsch - Oldenburg-Land" 29 "Cuxhaven - Stade II" 30 "Stade I - Rotenburg II" 31 "Mittelems" 32 "Cloppenburg - Vechta" 33 "Diephol- Nienburg I" 34 "Osterhol- Verden" 35 "Rotenburg I - Heidekreis" 36 "Harburg" 37 "Lüchow-Dannenberg - Lüneburg" 38 "Osnabrück-Land" 39 "Stadt Osnabrück" 40 "Nienburg II - Schaumburg" 41 "Stadt Hannover I" 42 "Stadt Hannover II" 43 "Hannover-Land I" 44 "Celle - Uelzen" 45 "Gifhorn - Peine" 46 "Hameln-Pyrmont - Holzminden" 47 "Hannover-Land II" 48 "Hildesheim" 49 "Salzgitter - Wolfenbüttel" 50 "Braunschweig" 51 "Helmstedt - Wolfsburg" 52 "Goslar - Northeim - Osterode" 53 "Göttingen" 54 "Bremen I" 55 "Bremen II - Bremerhaven" 56 "Prignit- Ostprignitz-Ruppin - Havelland I" 57 "Uckermark - Barnim I" 58 "Oberhavel - Havelland II" 59 "Märkisch-Oderland - Barnim II" 60 "Brandenburg an der Havel - Potsdam-Mittelmark I - Havelland III - Teltow-Fläming I" 61 "Potsdam - Potsdam-Mittelmark II - Teltow-Fläming II" 62 "Dahme-Spreewald - Teltow-Fläming III - Oberspreewald-LausitI" 63 "Frankfurt (Oder) - Oder-Spree" 64 "Cottbus - Spree-Neiße" 65 "Elbe-Elster - Oberspreewald-LausitII" 66 "Altmark" 67 "Börde - Jerichower Land" 68 "Harz" 69 "Magdeburg" 70 "Dessau - Wittenberg" 71 "Anhalt" 72 "Halle" 73 "Burgenland - Saalekreis" 74 "Mansfeld" 75 "Berlin-Mitte" 76 "Berlin-Pankow" 77 "Berlin-Reinickendorf" 78 "Berlin-Spandau - Charlottenburg Nord" 79 "Berlin-Steglit-ehlendorf" 80 "Berlin-Charlottenburg - Wilmersdorf" 81 "Berlin-Tempelhof - Schöneberg" 82 "Berlin-Neukölln" 83 "Berlin-Friedrichshain-Kreuzberg - Prenzlauer Berg Ost" 84 "Berlin-Treptow - Köpenick" 85 "Berlin-Marzahn - Hellersdorf" 86 "Berlin-Lichtenberg" 87 "Aachen I" 88 "Aachen II" 89 "Heinsberg" 90 "Düren" 91 "Rhein-Erft-Kreis I" 92 "Euskirchen - Rhein-Erft-Kreis II" 93 "Köln I" 94 "Köln II" 95 "Köln III" 96 "Bonn" 97 "Rhein-Sieg-Kreis I" 98 "Rhein-Sieg-Kreis II" 99 "Oberbergischer Kreis" 100 "Rheinisch-Bergischer Kreis" 101 "Leverkusen - Köln IV" 102 "Wuppertal I" 103 "Solingen - Remscheid - Wuppertal II" 104 "Mettmann I" 105 "Mettmann II" 106 "Düsseldorf I" 107 "Düsseldorf II" 108 "Neuss I" 109 "Mönchengladbach" 110 "Krefeld I - Neuss II" 111 "Viersen" 112 "Kleve" 113 "Wesel I" 114 "Krefeld II - Wesel II" 115 "Duisburg I" 116 "Duisburg II" 117 "Oberhausen - Wesel III" 118 "Mülheim - Essen I" 119 "Essen II" 120 "Essen III" 121 "Recklinghausen I" 122 "Recklinghausen II" 123 "Gelsenkirchen" 124 "Steinfurt I - Borken I" 125 "Bottrop - Recklinghausen III" 126 "Borken II" 127 "Coesfeld - Steinfurt II" 128 "Steinfurt III" 129 "Münster" 130 "Warendorf" 131 "Gütersloh I" 132 "Bielefeld - Gütersloh II" 133 "Herford - Minden-Lübbecke II" 134 "Minden-Lübbecke I" 135 "Lippe I" 136 "Höxter - Lippe II" 137 "Paderborn - Gütersloh III" 138 "Hagen - Ennepe-Ruhr-Kreis I" 139 "Ennepe-Ruhr-Kreis II" 140 "Bochum I" 141 "Herne - Bochum II" 142 "Dortmund I" 143 "Dortmund II" 144 "Unna I" 145 "Hamm - Unna II" 146 "Soest" 147 "Hochsauerlandkreis" 148 "Siegen-Wittgenstein" 149 "Olpe - Märkischer Kreis I" 150 "Märkischer Kreis II" 151 "Nordsachsen" 152 "Leipzig I" 153 "Leipzig II" 154 "Leipzig-Land" 155 "Meißen" 156 "Bautzen I" 157 "Görlitz" 158 "Sächsische Schwei- Osterzgebirge" 159 "Dresden I" 160 "Dresden II - Bautzen II" 161 "Mittelsachsen" 162 "Chemnitz" 163 "Chemnitzer Umland - Erzgebirgskreis II" 164 "Erzgebirgskreis I" 165 "Zwickau" 166 "Vogtlandkreis" 167 "Waldeck" 168 "Kassel" 169 "Werra-Meißner - Hersfeld-Rotenburg" 170 "Schwalm-Eder" 171 "Marburg" 172 "Lahn-Dill" 173 "Gießen" 174 "Fulda" 175 "Main-Kinzig - Wetterau II - Schotten" 176 "Hochtaunus" 177 "Wetterau I" 178 "Rheingau-Taunus - Limburg" 179 "Wiesbaden" 180 "Hanau" 181 "Main-Taunus" 182 "Frankfurt am Main I" 183 "Frankfurt am Main II" 184 "Groß-Gerau" 185 "Offenbach" 186 "Darmstadt" 187 "Odenwald" 188 "Bergstraße" 189 "Eichsfeld - Nordhausen - Unstrut-Hainich-Kreis I" 190 "Eisenach - Wartburgkreis - Unstrut-Hainich-Kreis II" 191 "Kyffhäuserkreis - Sömmerda - Weimarer Land I" 192 "Gotha - Ilm-Kreis" 193 "Erfurt - Weimar - Weimarer Land II" 194 "Gera - Jena - Saale-Holzland-Kreis" 195 "Grei- Altenburger Land" 196 "Sonneberg - Saalfeld-Rudolstadt - Saale-Orla-Kreis" 197 "Suhl - Schmalkalden-Meiningen - Hildburghausen" 198 "Neuwied" 199 "Ahrweiler" 200 "Koblenz" 201 "Mosel/Rhein-Hunsrück" 202 "Kreuznach" 203 "Bitburg" 204 "Trier" 205 "Montabaur" 206 "Mainz" 207 "Worms" 208 "Ludwigshafen/Frankenthal" 209 "Neustadt - Speyer" 210 "Kaiserslautern" 211 "Pirmasens" 212 "Südpfalz" 213 "Altötting" 214 "Erding - Ebersberg" 215 "Freising" 216 "Fürstenfeldbruck" 217 "Ingolstadt" 218 "München-Nord" 219 "München-Ost" 220 "München-Süd" 221 "München-West/Mitte" 222 "München-Land" 223 "Rosenheim" 224 "Starnberg" 225 "Traunstein" 226 "Weilheim" 227 "Deggendorf" 228 "Landshut" 229 "Passau" 230 "Rottal-Inn" 231 "Straubing" 232 "Amberg" 233 "Regensburg" 234 "Schwandorf" 235 "Weiden" 236 "Bamberg" 237 "Bayreuth" 238 "Coburg" 239 "Hof" 240 "Kulmbach" 241 "Ansbach" 242 "Erlangen" 243 "Fürth" 244 "Nürnberg-Nord" 245 "Nürnberg-Süd" 246 "Roth" 247 "Aschaffenburg" 248 "Bad Kissingen" 249 "Main-Spessart" 250 "Schweinfurt" 251 "Würzburg" 252 "Augsburg-Stadt" 253 "Augsburg-Land" 254 "Donau-Ries" 255 "Neu-Ulm" 256 "Oberallgäu" 257 "Ostallgäu" 258 "Stuttgart I" 259 "Stuttgart II" 260 "Böblingen" 261 "Esslingen" 262 "Nürtingen" 263 "Göppingen" 264 "Waiblingen" 265 "Ludwigsburg" 266 "Neckar-Zaber" 267 "Heilbronn" 268 "Schwäbisch Hall - Hohenlohe" 269 "Backnang - Schwäbisch Gmünd" 270 "Aalen - Heidenheim" 271 "Karlsruhe-Stadt" 272 "Karlsruhe-Land" 273 "Rastatt" 274 "Heidelberg" 275 "Mannheim" 276 "Odenwald - Tauber" 277 "Rhein-Neckar" 278 "Bruchsal - Schwetzingen" 279 "Pforzheim" 280 "Calw" 281 "Freiburg" 282 "Lörrach - Müllheim" 283 "Emmendingen - Lahr" 284 "Offenburg" 285 "Rottweil - Tuttlingen" 286 "Schwarzwald-Baar" 287 "Konstanz" 288 "Waldshut" 289 "Reutlingen" 290 "Tübingen" 291 "Ulm" 292 "Biberach" 293 "Bodensee" 294 "Ravensburg" 295 "Zollernalb - Sigmaringen" 296 "Saarbrücken" 297 "Saarlouis" 298 "St. Wendel" 299 "Homburg"

label values elecdist district


*****************************
** DISTRICT-LEVEL OUTCOMES **
*****************************

* Election outcome in each district (1 = CDU/CSU, 4 = SPD, 5 = FDP, 6 = Green, 7 = Die Linke)

gen district_outcome = .

replace district_outcome = 1 if elecdist == 1
replace district_outcome = 1 if elecdist == 2
replace district_outcome = 1 if elecdist == 3
replace district_outcome = 1 if elecdist == 4
replace district_outcome = 4 if elecdist == 5
replace district_outcome = 1 if elecdist == 6
replace district_outcome = 1 if elecdist == 7
replace district_outcome = 1 if elecdist == 8
replace district_outcome = 1 if elecdist == 9
replace district_outcome = 1 if elecdist == 10
replace district_outcome = 4 if elecdist == 11
replace district_outcome = 1 if elecdist == 12
replace district_outcome = 1 if elecdist == 13
replace district_outcome = 1 if elecdist == 14
replace district_outcome = 1 if elecdist == 15
replace district_outcome = 1 if elecdist == 16
replace district_outcome = 1 if elecdist == 17
replace district_outcome = 4 if elecdist == 18
replace district_outcome = 4 if elecdist == 19
replace district_outcome = 4 if elecdist == 20
replace district_outcome = 1 if elecdist == 21
replace district_outcome = 4 if elecdist == 22
replace district_outcome = 4 if elecdist == 23
replace district_outcome = 4 if elecdist == 24
replace district_outcome = 1 if elecdist == 25
replace district_outcome = 4 if elecdist == 26
replace district_outcome = 4 if elecdist == 27
replace district_outcome = 1 if elecdist == 28
replace district_outcome = 1 if elecdist == 29
replace district_outcome = 1 if elecdist == 30
replace district_outcome = 1 if elecdist == 31
replace district_outcome = 1 if elecdist == 32
replace district_outcome = 1 if elecdist == 33
replace district_outcome = 1 if elecdist == 34
replace district_outcome = 1 if elecdist == 35
replace district_outcome = 1 if elecdist == 36
replace district_outcome = 1 if elecdist == 37
replace district_outcome = 1 if elecdist == 38
replace district_outcome = 1 if elecdist == 39
replace district_outcome = 4 if elecdist == 40
replace district_outcome = 4 if elecdist == 41
replace district_outcome = 4 if elecdist == 42
replace district_outcome = 1 if elecdist == 43
replace district_outcome = 1 if elecdist == 44
replace district_outcome = 4 if elecdist == 45
replace district_outcome = 4 if elecdist == 46
replace district_outcome = 4 if elecdist == 47
replace district_outcome = 1 if elecdist == 48
replace district_outcome = 4 if elecdist == 49
replace district_outcome = 4 if elecdist == 50
replace district_outcome = 1 if elecdist == 51
replace district_outcome = 4 if elecdist == 52
replace district_outcome = 4 if elecdist == 53
replace district_outcome = 4 if elecdist == 54
replace district_outcome = 4 if elecdist == 55
replace district_outcome = 1 if elecdist == 56
replace district_outcome = 1 if elecdist == 57
replace district_outcome = 1 if elecdist == 58
replace district_outcome = 1 if elecdist == 59
replace district_outcome = 4 if elecdist == 60
replace district_outcome = 1 if elecdist == 61
replace district_outcome = 1 if elecdist == 62
replace district_outcome = 1 if elecdist == 63
replace district_outcome = 1 if elecdist == 64
replace district_outcome = 1 if elecdist == 65
replace district_outcome = 1 if elecdist == 66
replace district_outcome = 1 if elecdist == 67
replace district_outcome = 1 if elecdist == 68
replace district_outcome = 1 if elecdist == 69
replace district_outcome = 1 if elecdist == 70
replace district_outcome = 1 if elecdist == 71
replace district_outcome = 1 if elecdist == 72
replace district_outcome = 1 if elecdist == 73
replace district_outcome = 1 if elecdist == 74
replace district_outcome = 4 if elecdist == 75
replace district_outcome = 7 if elecdist == 76
replace district_outcome = 1 if elecdist == 77
replace district_outcome = 1 if elecdist == 78
replace district_outcome = 1 if elecdist == 79
replace district_outcome = 1 if elecdist == 80
replace district_outcome = 1 if elecdist == 81
replace district_outcome = 4 if elecdist == 82
replace district_outcome = 6 if elecdist == 83
replace district_outcome = 7 if elecdist == 84
replace district_outcome = 7 if elecdist == 85
replace district_outcome = 7 if elecdist == 86
replace district_outcome = 1 if elecdist == 87
replace district_outcome = 1 if elecdist == 88
replace district_outcome = 1 if elecdist == 89
replace district_outcome = 1 if elecdist == 90
replace district_outcome = 1 if elecdist == 91
replace district_outcome = 1 if elecdist == 92
replace district_outcome = 4 if elecdist == 93
replace district_outcome = 1 if elecdist == 94
replace district_outcome = 4 if elecdist == 95
replace district_outcome = 4 if elecdist == 96
replace district_outcome = 1 if elecdist == 97
replace district_outcome = 1 if elecdist == 98
replace district_outcome = 1 if elecdist == 99
replace district_outcome = 1 if elecdist == 100
replace district_outcome = 4 if elecdist == 101
replace district_outcome = 4 if elecdist == 102
replace district_outcome = 1 if elecdist == 103
replace district_outcome = 1 if elecdist == 104
replace district_outcome = 1 if elecdist == 105
replace district_outcome = 1 if elecdist == 106
replace district_outcome = 1 if elecdist == 107
replace district_outcome = 1 if elecdist == 108
replace district_outcome = 1 if elecdist == 109
replace district_outcome = 1 if elecdist == 110
replace district_outcome = 1 if elecdist == 111
replace district_outcome = 1 if elecdist == 112
replace district_outcome = 1 if elecdist == 113
replace district_outcome = 4 if elecdist == 114
replace district_outcome = 4 if elecdist == 115
replace district_outcome = 4 if elecdist == 116
replace district_outcome = 4 if elecdist == 117
replace district_outcome = 4 if elecdist == 118
replace district_outcome = 4 if elecdist == 119
replace district_outcome = 1 if elecdist == 120
replace district_outcome = 4 if elecdist == 121
replace district_outcome = 4 if elecdist == 122
replace district_outcome = 4 if elecdist == 123
replace district_outcome = 1 if elecdist == 124
replace district_outcome = 4 if elecdist == 125
replace district_outcome = 1 if elecdist == 126
replace district_outcome = 1 if elecdist == 127
replace district_outcome = 1 if elecdist == 128
replace district_outcome = 1 if elecdist == 129
replace district_outcome = 1 if elecdist == 130
replace district_outcome = 1 if elecdist == 131
replace district_outcome = 4 if elecdist == 132
replace district_outcome = 4 if elecdist == 133
replace district_outcome = 1 if elecdist == 134
replace district_outcome = 4 if elecdist == 135
replace district_outcome = 1 if elecdist == 136
replace district_outcome = 1 if elecdist == 137
replace district_outcome = 4 if elecdist == 138
replace district_outcome = 4 if elecdist == 139
replace district_outcome = 4 if elecdist == 140
replace district_outcome = 4 if elecdist == 141
replace district_outcome = 4 if elecdist == 142
replace district_outcome = 4 if elecdist == 143
replace district_outcome = 4 if elecdist == 144
replace district_outcome = 4 if elecdist == 145
replace district_outcome = 1 if elecdist == 146
replace district_outcome = 1 if elecdist == 147
replace district_outcome = 1 if elecdist == 148
replace district_outcome = 1 if elecdist == 149
replace district_outcome = 4 if elecdist == 150
replace district_outcome = 1 if elecdist == 151
replace district_outcome = 1 if elecdist == 152
replace district_outcome = 1 if elecdist == 153
replace district_outcome = 1 if elecdist == 154
replace district_outcome = 1 if elecdist == 155
replace district_outcome = 1 if elecdist == 156
replace district_outcome = 1 if elecdist == 157
replace district_outcome = 1 if elecdist == 158
replace district_outcome = 1 if elecdist == 159
replace district_outcome = 1 if elecdist == 160
replace district_outcome = 1 if elecdist == 161
replace district_outcome = 1 if elecdist == 162
replace district_outcome = 1 if elecdist == 163
replace district_outcome = 1 if elecdist == 164
replace district_outcome = 1 if elecdist == 165
replace district_outcome = 1 if elecdist == 166
replace district_outcome = 1 if elecdist == 167
replace district_outcome = 4 if elecdist == 168
replace district_outcome = 4 if elecdist == 169
replace district_outcome = 4 if elecdist == 170
replace district_outcome = 4 if elecdist == 171
replace district_outcome = 1 if elecdist == 172
replace district_outcome = 1 if elecdist == 173
replace district_outcome = 1 if elecdist == 174
replace district_outcome = 1 if elecdist == 175
replace district_outcome = 1 if elecdist == 176
replace district_outcome = 1 if elecdist == 177
replace district_outcome = 1 if elecdist == 178
replace district_outcome = 1 if elecdist == 179
replace district_outcome = 1 if elecdist == 180
replace district_outcome = 1 if elecdist == 181
replace district_outcome = 1 if elecdist == 182
replace district_outcome = 1 if elecdist == 183
replace district_outcome = 1 if elecdist == 184
replace district_outcome = 1 if elecdist == 185
replace district_outcome = 4 if elecdist == 186
replace district_outcome = 1 if elecdist == 187
replace district_outcome = 1 if elecdist == 188
replace district_outcome = 1 if elecdist == 189
replace district_outcome = 1 if elecdist == 190
replace district_outcome = 1 if elecdist == 191
replace district_outcome = 1 if elecdist == 192
replace district_outcome = 1 if elecdist == 193
replace district_outcome = 1 if elecdist == 194
replace district_outcome = 1 if elecdist == 195
replace district_outcome = 1 if elecdist == 196
replace district_outcome = 1 if elecdist == 197
replace district_outcome = 1 if elecdist == 198
replace district_outcome = 1 if elecdist == 199
replace district_outcome = 1 if elecdist == 200
replace district_outcome = 1 if elecdist == 201
replace district_outcome = 1 if elecdist == 202
replace district_outcome = 1 if elecdist == 203
replace district_outcome = 1 if elecdist == 204
replace district_outcome = 1 if elecdist == 205
replace district_outcome = 1 if elecdist == 206
replace district_outcome = 1 if elecdist == 207
replace district_outcome = 1 if elecdist == 208
replace district_outcome = 1 if elecdist == 209
replace district_outcome = 4 if elecdist == 210
replace district_outcome = 1 if elecdist == 211
replace district_outcome = 1 if elecdist == 212
replace district_outcome = 1 if elecdist == 213
replace district_outcome = 1 if elecdist == 214
replace district_outcome = 1 if elecdist == 215
replace district_outcome = 1 if elecdist == 216
replace district_outcome = 1 if elecdist == 217
replace district_outcome = 1 if elecdist == 218
replace district_outcome = 1 if elecdist == 219
replace district_outcome = 1 if elecdist == 220
replace district_outcome = 1 if elecdist == 221
replace district_outcome = 1 if elecdist == 222
replace district_outcome = 1 if elecdist == 223
replace district_outcome = 1 if elecdist == 224
replace district_outcome = 1 if elecdist == 225
replace district_outcome = 1 if elecdist == 226
replace district_outcome = 1 if elecdist == 227
replace district_outcome = 1 if elecdist == 228
replace district_outcome = 1 if elecdist == 229
replace district_outcome = 1 if elecdist == 230
replace district_outcome = 1 if elecdist == 231
replace district_outcome = 1 if elecdist == 232
replace district_outcome = 1 if elecdist == 233
replace district_outcome = 1 if elecdist == 234
replace district_outcome = 1 if elecdist == 235
replace district_outcome = 1 if elecdist == 236
replace district_outcome = 1 if elecdist == 237
replace district_outcome = 1 if elecdist == 238
replace district_outcome = 1 if elecdist == 239
replace district_outcome = 1 if elecdist == 240
replace district_outcome = 1 if elecdist == 241
replace district_outcome = 1 if elecdist == 242
replace district_outcome = 1 if elecdist == 243
replace district_outcome = 1 if elecdist == 244
replace district_outcome = 1 if elecdist == 245
replace district_outcome = 1 if elecdist == 246
replace district_outcome = 1 if elecdist == 247
replace district_outcome = 1 if elecdist == 248
replace district_outcome = 1 if elecdist == 249
replace district_outcome = 1 if elecdist == 250
replace district_outcome = 1 if elecdist == 251
replace district_outcome = 1 if elecdist == 252
replace district_outcome = 1 if elecdist == 253
replace district_outcome = 1 if elecdist == 254
replace district_outcome = 1 if elecdist == 255
replace district_outcome = 1 if elecdist == 256
replace district_outcome = 1 if elecdist == 257
replace district_outcome = 1 if elecdist == 258
replace district_outcome = 1 if elecdist == 259
replace district_outcome = 1 if elecdist == 260
replace district_outcome = 1 if elecdist == 261
replace district_outcome = 1 if elecdist == 262
replace district_outcome = 1 if elecdist == 263
replace district_outcome = 1 if elecdist == 264
replace district_outcome = 1 if elecdist == 265
replace district_outcome = 1 if elecdist == 266
replace district_outcome = 1 if elecdist == 267
replace district_outcome = 1 if elecdist == 268
replace district_outcome = 1 if elecdist == 269
replace district_outcome = 1 if elecdist == 270
replace district_outcome = 1 if elecdist == 271
replace district_outcome = 1 if elecdist == 272
replace district_outcome = 1 if elecdist == 273
replace district_outcome = 1 if elecdist == 274
replace district_outcome = 1 if elecdist == 275
replace district_outcome = 1 if elecdist == 276
replace district_outcome = 1 if elecdist == 277
replace district_outcome = 1 if elecdist == 278
replace district_outcome = 1 if elecdist == 279
replace district_outcome = 1 if elecdist == 280
replace district_outcome = 1 if elecdist == 281
replace district_outcome = 1 if elecdist == 282
replace district_outcome = 1 if elecdist == 283
replace district_outcome = 1 if elecdist == 284
replace district_outcome = 1 if elecdist == 285
replace district_outcome = 1 if elecdist == 286
replace district_outcome = 1 if elecdist == 287
replace district_outcome = 1 if elecdist == 288
replace district_outcome = 1 if elecdist == 289
replace district_outcome = 1 if elecdist == 290
replace district_outcome = 1 if elecdist == 291
replace district_outcome = 1 if elecdist == 292
replace district_outcome = 1 if elecdist == 293
replace district_outcome = 1 if elecdist == 294
replace district_outcome = 1 if elecdist == 295
replace district_outcome = 1 if elecdist == 296
replace district_outcome = 1 if elecdist == 297
replace district_outcome = 1 if elecdist == 298
replace district_outcome = 1 if elecdist == 299


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 5.305683 if elecdist == 1
replace margin = 17.68107 if elecdist == 2
replace margin = 11.36477 if elecdist == 3
replace margin = 8.372566 if elecdist == 4
replace margin = 9.908962 if elecdist == 5
replace margin = 6.178036 if elecdist == 6
replace margin = 9.290565 if elecdist == 7
replace margin = 10.17712 if elecdist == 8
replace margin = 8.769211 if elecdist == 9
replace margin = 10.59446 if elecdist == 10
replace margin = 4.241238 if elecdist == 11
replace margin = 13.07389 if elecdist == 12
replace margin = 20.10578 if elecdist == 13
replace margin = 4.329643 if elecdist == 14
replace margin = 36.98003 if elecdist == 15
replace margin = 21.9698 if elecdist == 16
replace margin = 24.36958 if elecdist == 17
replace margin = 10.98904 if elecdist == 18
replace margin = 2.461998 if elecdist == 19
replace margin = 4.23719 if elecdist == 20
replace margin = 4.953049 if elecdist == 21
replace margin = 2.773552 if elecdist == 22
replace margin = 4.852257 if elecdist == 23
replace margin = 17.88184 if elecdist == 24
replace margin = 23.55107 if elecdist == 25
replace margin = 4.478992 if elecdist == 26
replace margin = 0.6316643 if elecdist == 27
replace margin = 0.3585663 if elecdist == 28
replace margin = 10.34638 if elecdist == 29
replace margin = 13.49865 if elecdist == 30
replace margin = 30.08296 if elecdist == 31
replace margin = 45.02241 if elecdist == 32
replace margin = 14.05356 if elecdist == 33
replace margin = 6.633617 if elecdist == 34
replace margin = 4.236004 if elecdist == 35
replace margin = 13.57305 if elecdist == 36
replace margin = 6.167217 if elecdist == 37
replace margin = 20.78167 if elecdist == 38
replace margin = 10.89759 if elecdist == 39
replace margin = 3.058964 if elecdist == 40
replace margin = 7.080067 if elecdist == 41
replace margin = 8.974915 if elecdist == 42
replace margin = 5.753288 if elecdist == 43
replace margin = 14.23317 if elecdist == 44
replace margin = 1.180744 if elecdist == 45
replace margin = 2.76804 if elecdist == 46
replace margin = 1.477093 if elecdist == 47
replace margin = 1.026825 if elecdist == 48
replace margin = 9.548443 if elecdist == 49
replace margin = 8.61488 if elecdist == 50
replace margin = 6.040718 if elecdist == 51
replace margin = 0.3309975 if elecdist == 52
replace margin = 2.218018 if elecdist == 53
replace margin = 7.655823 if elecdist == 54
replace margin = 15.16728 if elecdist == 55
replace margin = 3.966663 if elecdist == 56
replace margin = 12.88137 if elecdist == 57
replace margin = 9.448017 if elecdist == 58
replace margin = 1.062679 if elecdist == 59
replace margin = 0.2735291 if elecdist == 60
replace margin = 0.4075012 if elecdist == 61
replace margin = 9.884823 if elecdist == 62
replace margin = 5.914053 if elecdist == 63
replace margin = 12.00266 if elecdist == 64
replace margin = 17.51284 if elecdist == 65
replace margin = 11.80897 if elecdist == 66
replace margin = 21.4437 if elecdist == 67
replace margin = 23.02453 if elecdist == 68
replace margin = 11.09403 if elecdist == 69
replace margin = 22.72273 if elecdist == 70
replace margin = 12.17734 if elecdist == 71
replace margin = 10.76891 if elecdist == 72
replace margin = 19.109 if elecdist == 73
replace margin = 14.35692 if elecdist == 74
replace margin = 4.3757 if elecdist == 75
replace margin = 4.420002 if elecdist == 76
replace margin = 15.4736 if elecdist == 77
replace margin = 1.785336 if elecdist == 78
replace margin = 13.25009 if elecdist == 79
replace margin = 5.590284 if elecdist == 80
replace margin = 8.766127 if elecdist == 81
replace margin = 1.76877 if elecdist == 82
replace margin = 21.87702 if elecdist == 83
replace margin = 19.5768 if elecdist == 84
replace margin = 13.24318 if elecdist == 85
replace margin = 17.56816 if elecdist == 86
replace margin = 5.465855 if elecdist == 87
replace margin = 9.895447 if elecdist == 88
replace margin = 25.09522 if elecdist == 89
replace margin = 16.14697 if elecdist == 90
replace margin = 11.26969 if elecdist == 91
replace margin = 20.11499 if elecdist == 92
replace margin = 0.3044319 if elecdist == 93
replace margin = 7.135818 if elecdist == 94
replace margin = 6.446636 if elecdist == 95
replace margin = 0.6808548 if elecdist == 96
replace margin = 20.12007 if elecdist == 97
replace margin = 25.21469 if elecdist == 98
replace margin = 22.17827 if elecdist == 99
replace margin = 33.48548 if elecdist == 100
replace margin = 2.00819 if elecdist == 101
replace margin = 4.17001 if elecdist == 102
replace margin = 10.93383 if elecdist == 103
replace margin = 14.93126 if elecdist == 104
replace margin = 8.416615 if elecdist == 105
replace margin = 18.65135 if elecdist == 106
replace margin = 5.767536 if elecdist == 107
replace margin = 21.03549 if elecdist == 108
replace margin = 21.6375 if elecdist == 109
replace margin = 18.13921 if elecdist == 110
replace margin = 24.49578 if elecdist == 111
replace margin = 17.83059 if elecdist == 112
replace margin = 4.245132 if elecdist == 113
replace margin = 1.348686 if elecdist == 114
replace margin = 14.46003 if elecdist == 115
replace margin = 13.61237 if elecdist == 116
replace margin = 12.77666 if elecdist == 117
replace margin = 6.649658 if elecdist == 118
replace margin = 17.23045 if elecdist == 119
replace margin = 0.0622139 if elecdist == 120
replace margin = 10.88473 if elecdist == 121
replace margin = 9.129841 if elecdist == 122
replace margin = 21.82996 if elecdist == 123
replace margin = 20.154 if elecdist == 124
replace margin = 9.276123 if elecdist == 125
replace margin = 30.25949 if elecdist == 126
replace margin = 29.2834 if elecdist == 127
replace margin = 11.47021 if elecdist == 128
replace margin = 3.39146 if elecdist == 129
replace margin = 19.31957 if elecdist == 130
replace margin = 16.67915 if elecdist == 131
replace margin = 0.8145676 if elecdist == 132
replace margin = 0.4735565 if elecdist == 133
replace margin = 5.712635 if elecdist == 134
replace margin = 0.9288902 if elecdist == 135
replace margin = 18.68357 if elecdist == 136
replace margin = 35.87442 if elecdist == 137
replace margin = 12.94367 if elecdist == 138
replace margin = 6.5238 if elecdist == 139
replace margin = 8.593258 if elecdist == 140
replace margin = 18.31125 if elecdist == 141
replace margin = 14.72106 if elecdist == 142
replace margin = 14.52235 if elecdist == 143
replace margin = 10.83841 if elecdist == 144
replace margin = 3.822037 if elecdist == 145
replace margin = 16.47792 if elecdist == 146
replace margin = 26.2493 if elecdist == 147
replace margin = 9.255154 if elecdist == 148
replace margin = 18.9469 if elecdist == 149
replace margin = 0.0369339 if elecdist == 150
replace margin = 23.81031 if elecdist == 151
replace margin = 16.60056 if elecdist == 152
replace margin = 9.434349 if elecdist == 153
replace margin = 29.85613 if elecdist == 154
replace margin = 35.94175 if elecdist == 155
replace margin = 27.85603 if elecdist == 156
replace margin = 29.83238 if elecdist == 157
replace margin = 31.134 if elecdist == 158
replace margin = 17.57286 if elecdist == 159
replace margin = 22.67574 if elecdist == 160
replace margin = 31.56253 if elecdist == 161
replace margin = 17.82523 if elecdist == 162
replace margin = 27.67241 if elecdist == 163
replace margin = 29.66476 if elecdist == 164
replace margin = 19.92692 if elecdist == 165
replace margin = 27.59854 if elecdist == 166
replace margin = 0.1697807 if elecdist == 167
replace margin = 4.826874 if elecdist == 168
replace margin = 2.558075 if elecdist == 169
replace margin = 2.764034 if elecdist == 170
replace margin = 4.139778 if elecdist == 171
replace margin = 13.61921 if elecdist == 172
replace margin = 8.902706 if elecdist == 173
replace margin = 33.36386 if elecdist == 174
replace margin = 16.44672 if elecdist == 175
replace margin = 20.69721 if elecdist == 176
replace margin = 15.11544 if elecdist == 177
replace margin = 23.04298 if elecdist == 178
replace margin = 8.605259 if elecdist == 179
replace margin = 7.845222 if elecdist == 180
replace margin = 25.43639 if elecdist == 181
replace margin = 7.127697 if elecdist == 182
replace margin = 5.828878 if elecdist == 183
replace margin = 4.172611 if elecdist == 184
replace margin = 14.28748 if elecdist == 185
replace margin = 1.372051 if elecdist == 186
replace margin = 13.27708 if elecdist == 187
replace margin = 16.55906 if elecdist == 188
replace margin = 30.06932 if elecdist == 189
replace margin = 19.59371 if elecdist == 190
replace margin = 16.83238 if elecdist == 191
replace margin = 12.32087 if elecdist == 192
replace margin = 13.20814 if elecdist == 193
replace margin = 9.107 if elecdist == 194
replace margin = 20.56798 if elecdist == 195
replace margin = 14.76752 if elecdist == 196
replace margin = 14.6516 if elecdist == 197
replace margin = 9.590755 if elecdist == 198
replace margin = 27.4627 if elecdist == 199
replace margin = 17.14593 if elecdist == 200
replace margin = 25.77229 if elecdist == 201
replace margin = 3.726433 if elecdist == 202
replace margin = 29.70958 if elecdist == 203
replace margin = 17.7066 if elecdist == 204
replace margin = 18.31024 if elecdist == 205
replace margin = 5.255257 if elecdist == 206
replace margin = 5.061825 if elecdist == 207
replace margin = 7.737774 if elecdist == 208
replace margin = 19.26595 if elecdist == 209
replace margin = 1.36301 if elecdist == 210
replace margin = 13.65306 if elecdist == 211
replace margin = 20.86374 if elecdist == 212
replace margin = 51.41032 if elecdist == 213
replace margin = 36.31045 if elecdist == 214
replace margin = 35.32977 if elecdist == 215
replace margin = 35.46247 if elecdist == 216
replace margin = 45.06208 if elecdist == 217
replace margin = 11.82822 if elecdist == 218
replace margin = 15.94243 if elecdist == 219
replace margin = 14.97803 if elecdist == 220
replace margin = 14.01118 if elecdist == 221
replace margin = 32.16292 if elecdist == 222
replace margin = 45.62188 if elecdist == 223
replace margin = 36.4817 if elecdist == 224
replace margin = 45.04832 if elecdist == 225
replace margin = 40.73023 if elecdist == 226
replace margin = 45.50688 if elecdist == 227
replace margin = 41.86334 if elecdist == 228
replace margin = 40.11937 if elecdist == 229
replace margin = 44.94208 if elecdist == 230
replace margin = 43.65226 if elecdist == 231
replace margin = 37.46011 if elecdist == 232
replace margin = 26.44641 if elecdist == 233
replace margin = 32.91821 if elecdist == 234
replace margin = 33.15165 if elecdist == 235
replace margin = 29.33807 if elecdist == 236
replace margin = 30.21874 if elecdist == 237
replace margin = 17.68591 if elecdist == 238
replace margin = 26.46052 if elecdist == 239
replace margin = 34.12233 if elecdist == 240
replace margin = 30.07849 if elecdist == 241
replace margin = 22.16597 if elecdist == 242
replace margin = 22.0882 if elecdist == 243
replace margin = 7.721415 if elecdist == 244
replace margin = 12.08136 if elecdist == 245
replace margin = 22.97741 if elecdist == 246
replace margin = 27.37238 if elecdist == 247
replace margin = 38.00291 if elecdist == 248
replace margin = 28.48001 if elecdist == 249
replace margin = 31.78372 if elecdist == 250
replace margin = 23.38573 if elecdist == 251
replace margin = 19.29725 if elecdist == 252
replace margin = 42.92397 if elecdist == 253
replace margin = 42.98698 if elecdist == 254
replace margin = 39.10841 if elecdist == 255
replace margin = 43.32918 if elecdist == 256
replace margin = 44.71638 if elecdist == 257
replace margin = 14.50796 if elecdist == 258
replace margin = 17.71521 if elecdist == 259
replace margin = 30.58665 if elecdist == 260
replace margin = 26.04321 if elecdist == 261
replace margin = 25.69693 if elecdist == 262
replace margin = 24.825 if elecdist == 263
replace margin = 28.43819 if elecdist == 264
replace margin = 29.24621 if elecdist == 265
replace margin = 26.81375 if elecdist == 266
replace margin = 24.31197 if elecdist == 267
replace margin = 29.33843 if elecdist == 268
replace margin = 30.57823 if elecdist == 269
replace margin = 32.99544 if elecdist == 270
replace margin = 9.848021 if elecdist == 271
replace margin = 27.41598 if elecdist == 272
replace margin = 30.4629 if elecdist == 273
replace margin = 10.34819 if elecdist == 274
replace margin = 7.45966 if elecdist == 275
replace margin = 37.79105 if elecdist == 276
replace margin = 23.73483 if elecdist == 277
replace margin = 27.12582 if elecdist == 278
replace margin = 26.51323 if elecdist == 279
replace margin = 38.27509 if elecdist == 280
replace margin = 4.865072 if elecdist == 281
replace margin = 25.48891 if elecdist == 282
replace margin = 25.91043 if elecdist == 283
replace margin = 35.85072 if elecdist == 284
replace margin = 40.07166 if elecdist == 285
replace margin = 37.74006 if elecdist == 286
replace margin = 32.65911 if elecdist == 287
replace margin = 23.44967 if elecdist == 288
replace margin = 31.7361 if elecdist == 289
replace margin = 26.48619 if elecdist == 290
replace margin = 28.27273 if elecdist == 291
replace margin = 39.09622 if elecdist == 292
replace margin = 33.31177 if elecdist == 293
replace margin = 31.58581 if elecdist == 294
replace margin = 42.46223 if elecdist == 295
replace margin = 0.7464867 if elecdist == 296
replace margin = 9.598671 if elecdist == 297
replace margin = 11.93307 if elecdist == 298
replace margin = 2.148075 if elecdist == 299

* Effective number of electoral parties by district

gen enep = .

replace enep = 3.037747 if elecdist == 1
replace enep = 2.788599 if elecdist == 2
replace enep = 3.029313 if elecdist == 3
replace enep = 2.879683 if elecdist == 4
replace enep = 3.234163 if elecdist == 5
replace enep = 2.942914 if elecdist == 6
replace enep = 2.907004 if elecdist == 7
replace enep = 2.955484 if elecdist == 8
replace enep = 2.824008 if elecdist == 9
replace enep = 2.998411 if elecdist == 10
replace enep = 3.228178 if elecdist == 11
replace enep = 3.677427 if elecdist == 12
replace enep = 3.42799 if elecdist == 13
replace enep = 3.874736 if elecdist == 14
replace enep = 2.663679 if elecdist == 15
replace enep = 3.357082 if elecdist == 16
replace enep = 3.221576 if elecdist == 17
replace enep = 3.864727 if elecdist == 18
replace enep = 3.877833 if elecdist == 19
replace enep = 3.62822 if elecdist == 20
replace enep = 3.362628 if elecdist == 21
replace enep = 3.233257 if elecdist == 22
replace enep = 3.305823 if elecdist == 23
replace enep = 2.708701 if elecdist == 24
replace enep = 2.488366 if elecdist == 25
replace enep = 2.785639 if elecdist == 26
replace enep = 3.405746 if elecdist == 27
replace enep = 3.111946 if elecdist == 28
replace enep = 2.795765 if elecdist == 29
replace enep = 2.854408 if elecdist == 30
replace enep = 2.291373 if elecdist == 31
replace enep = 2.049642 if elecdist == 32
replace enep = 2.895598 if elecdist == 33
replace enep = 2.918975 if elecdist == 34
replace enep = 2.700727 if elecdist == 35
replace enep = 3.166887 if elecdist == 36
replace enep = 3.424498 if elecdist == 37
replace enep = 2.552296 if elecdist == 38
replace enep = 2.931214 if elecdist == 39
replace enep = 2.649111 if elecdist == 40
replace enep = 3.007774 if elecdist == 41
replace enep = 3.186361 if elecdist == 42
replace enep = 2.854823 if elecdist == 43
replace enep = 2.782252 if elecdist == 44
replace enep = 2.711233 if elecdist == 45
replace enep = 2.909281 if elecdist == 46
replace enep = 2.713454 if elecdist == 47
replace enep = 2.78451 if elecdist == 48
replace enep = 2.780113 if elecdist == 49
replace enep = 3.103863 if elecdist == 50
replace enep = 2.812402 if elecdist == 51
replace enep = 2.762582 if elecdist == 52
replace enep = 3.094779 if elecdist == 53
replace enep = 3.790055 if elecdist == 54
replace enep = 3.410464 if elecdist == 55
replace enep = 3.825345 if elecdist == 56
replace enep = 3.608803 if elecdist == 57
replace enep = 3.814347 if elecdist == 58
replace enep = 3.720127 if elecdist == 59
replace enep = 3.61523 if elecdist == 60
replace enep = 3.890588 if elecdist == 61
replace enep = 3.807481 if elecdist == 62
replace enep = 3.871401 if elecdist == 63
replace enep = 4.22646 if elecdist == 64
replace enep = 3.607792 if elecdist == 65
replace enep = 3.303895 if elecdist == 66
replace enep = 3.323083 if elecdist == 67
replace enep = 3.289501 if elecdist == 68
replace enep = 3.813008 if elecdist == 69
replace enep = 3.546929 if elecdist == 70
replace enep = 3.589401 if elecdist == 71
replace enep = 3.92168 if elecdist == 72
replace enep = 3.38533 if elecdist == 73
replace enep = 3.476175 if elecdist == 74
replace enep = 4.9578 if elecdist == 75
replace enep = 4.841611 if elecdist == 76
replace enep = 3.319669 if elecdist == 77
replace enep = 3.287124 if elecdist == 78
replace enep = 3.501164 if elecdist == 79
replace enep = 3.783114 if elecdist == 80
replace enep = 4.155165 if elecdist == 81
replace enep = 4.361328 if elecdist == 82
replace enep = 4.135545 if elecdist == 83
replace enep = 3.750613 if elecdist == 84
replace enep = 3.959516 if elecdist == 85
replace enep = 3.872539 if elecdist == 86
replace enep = 3.284156 if elecdist == 87
replace enep = 2.914431 if elecdist == 88
replace enep = 2.687607 if elecdist == 89
replace enep = 2.651246 if elecdist == 90
replace enep = 2.772357 if elecdist == 91
replace enep = 2.761653 if elecdist == 92
replace enep = 3.462074 if elecdist == 93
replace enep = 3.416439 if elecdist == 94
replace enep = 3.486098 if elecdist == 95
replace enep = 3.332621 if elecdist == 96
replace enep = 2.926147 if elecdist == 97
replace enep = 2.80406 if elecdist == 98
replace enep = 2.705294 if elecdist == 99
replace enep = 2.427084 if elecdist == 100
replace enep = 2.982905 if elecdist == 101
replace enep = 3.198567 if elecdist == 102
replace enep = 3.13389 if elecdist == 103
replace enep = 2.696671 if elecdist == 104
replace enep = 2.840125 if elecdist == 105
replace enep = 3.061929 if elecdist == 106
replace enep = 3.288052 if elecdist == 107
replace enep = 2.736772 if elecdist == 108
replace enep = 2.832671 if elecdist == 109
replace enep = 2.904393 if elecdist == 110
replace enep = 2.709805 if elecdist == 111
replace enep = 2.666014 if elecdist == 112
replace enep = 2.862369 if elecdist == 113
replace enep = 2.941177 if elecdist == 114
replace enep = 3.016016 if elecdist == 115
replace enep = 3.459939 if elecdist == 116
replace enep = 3.116646 if elecdist == 117
replace enep = 3.179189 if elecdist == 118
replace enep = 2.945446 if elecdist == 119
replace enep = 3.103504 if elecdist == 120
replace enep = 3.021378 if elecdist == 121
replace enep = 2.864231 if elecdist == 122
replace enep = 2.902 if elecdist == 123
replace enep = 2.647119 if elecdist == 124
replace enep = 2.862754 if elecdist == 125
replace enep = 2.447084 if elecdist == 126
replace enep = 2.542429 if elecdist == 127
replace enep = 2.711713 if elecdist == 128
replace enep = 3.393479 if elecdist == 129
replace enep = 2.687605 if elecdist == 130
replace enep = 2.693047 if elecdist == 131
replace enep = 3.312958 if elecdist == 132
replace enep = 2.899006 if elecdist == 133
replace enep = 2.612467 if elecdist == 134
replace enep = 2.965292 if elecdist == 135
replace enep = 2.819075 if elecdist == 136
replace enep = 2.436745 if elecdist == 137
replace enep = 2.894995 if elecdist == 138
replace enep = 3.142004 if elecdist == 139
replace enep = 2.99999 if elecdist == 140
replace enep = 2.940309 if elecdist == 141
replace enep = 3.19852 if elecdist == 142
replace enep = 3.002692 if elecdist == 143
replace enep = 2.828132 if elecdist == 144
replace enep = 2.873323 if elecdist == 145
replace enep = 2.730519 if elecdist == 146
replace enep = 2.447519 if elecdist == 147
replace enep = 2.863429 if elecdist == 148
replace enep = 2.641951 if elecdist == 149
replace enep = 2.835639 if elecdist == 150
replace enep = 3.319493 if elecdist == 151
replace enep = 3.742046 if elecdist == 152
replace enep = 4.100692 if elecdist == 153
replace enep = 2.963001 if elecdist == 154
replace enep = 2.943158 if elecdist == 155
replace enep = 3.262314 if elecdist == 156
replace enep = 3.192212 if elecdist == 157
replace enep = 3.237643 if elecdist == 158
replace enep = 3.668642 if elecdist == 159
replace enep = 4.036956 if elecdist == 160
replace enep = 2.951907 if elecdist == 161
replace enep = 3.586104 if elecdist == 162
replace enep = 3.099397 if elecdist == 163
replace enep = 3.14702 if elecdist == 164
replace enep = 3.465958 if elecdist == 165
replace enep = 3.144277 if elecdist == 166
replace enep = 2.850679 if elecdist == 167
replace enep = 3.29604 if elecdist == 168
replace enep = 2.804017 if elecdist == 169
replace enep = 2.913466 if elecdist == 170
replace enep = 2.815097 if elecdist == 171
replace enep = 2.801034 if elecdist == 172
replace enep = 3.006228 if elecdist == 173
replace enep = 2.444014 if elecdist == 174
replace enep = 2.846426 if elecdist == 175
replace enep = 3.046577 if elecdist == 176
replace enep = 3.001158 if elecdist == 177
replace enep = 2.747252 if elecdist == 178
replace enep = 3.101505 if elecdist == 179
replace enep = 2.969694 if elecdist == 180
replace enep = 2.774168 if elecdist == 181
replace enep = 3.468453 if elecdist == 182
replace enep = 3.994511 if elecdist == 183
replace enep = 2.985217 if elecdist == 184
replace enep = 3.150614 if elecdist == 185
replace enep = 3.482523 if elecdist == 186
replace enep = 2.92078 if elecdist == 187
replace enep = 2.924415 if elecdist == 188
replace enep = 3.142324 if elecdist == 189
replace enep = 3.542347 if elecdist == 190
replace enep = 3.425128 if elecdist == 191
replace enep = 4.024896 if elecdist == 192
replace enep = 3.914166 if elecdist == 193
replace enep = 4.107324 if elecdist == 194
replace enep = 3.489321 if elecdist == 195
replace enep = 3.538087 if elecdist == 196
replace enep = 3.471503 if elecdist == 197
replace enep = 2.751823 if elecdist == 198
replace enep = 2.545176 if elecdist == 199
replace enep = 2.970796 if elecdist == 200
replace enep = 2.692307 if elecdist == 201
replace enep = 3.117157 if elecdist == 202
replace enep = 2.578302 if elecdist == 203
replace enep = 2.902708 if elecdist == 204
replace enep = 2.87688 if elecdist == 205
replace enep = 3.350575 if elecdist == 206
replace enep = 3.117223 if elecdist == 207
replace enep = 3.109164 if elecdist == 208
replace enep = 3.104962 if elecdist == 209
replace enep = 3.358407 if elecdist == 210
replace enep = 3.099947 if elecdist == 211
replace enep = 3.06124 if elecdist == 212
replace enep = 2.173757 if elecdist == 213
replace enep = 2.813523 if elecdist == 214
replace enep = 3.024496 if elecdist == 215
replace enep = 2.776603 if elecdist == 216
replace enep = 2.413298 if elecdist == 217
replace enep = 3.324149 if elecdist == 218
replace enep = 3.36951 if elecdist == 219
replace enep = 3.482726 if elecdist == 220
replace enep = 3.510323 if elecdist == 221
replace enep = 2.994061 if elecdist == 222
replace enep = 2.719306 if elecdist == 223
replace enep = 2.93287 if elecdist == 224
replace enep = 2.310393 if elecdist == 225
replace enep = 2.727084 if elecdist == 226
replace enep = 2.441546 if elecdist == 227
replace enep = 2.655487 if elecdist == 228
replace enep = 2.479842 if elecdist == 229
replace enep = 2.451291 if elecdist == 230
replace enep = 2.428623 if elecdist == 231
replace enep = 2.555395 if elecdist == 232
replace enep = 3.361632 if elecdist == 233
replace enep = 2.503599 if elecdist == 234
replace enep = 2.784566 if elecdist == 235
replace enep = 2.970534 if elecdist == 236
replace enep = 2.598585 if elecdist == 237
replace enep = 2.770219 if elecdist == 238
replace enep = 2.548587 if elecdist == 239
replace enep = 2.623358 if elecdist == 240
replace enep = 2.874591 if elecdist == 241
replace enep = 3.150101 if elecdist == 242
replace enep = 3.05963 if elecdist == 243
replace enep = 3.669185 if elecdist == 244
replace enep = 3.215955 if elecdist == 245
replace enep = 2.924753 if elecdist == 246
replace enep = 2.875249 if elecdist == 247
replace enep = 2.592565 if elecdist == 248
replace enep = 3.010738 if elecdist == 249
replace enep = 2.757406 if elecdist == 250
replace enep = 3.144346 if elecdist == 251
replace enep = 3.57911 if elecdist == 252
replace enep = 2.453591 if elecdist == 253
replace enep = 2.451059 if elecdist == 254
replace enep = 2.661127 if elecdist == 255
replace enep = 2.443979 if elecdist == 256
replace enep = 2.571144 if elecdist == 257
replace enep = 3.540588 if elecdist == 258
replace enep = 3.510942 if elecdist == 259
replace enep = 2.754999 if elecdist == 260
replace enep = 2.925074 if elecdist == 261
replace enep = 2.969149 if elecdist == 262
replace enep = 3.16099 if elecdist == 263
replace enep = 3.002987 if elecdist == 264
replace enep = 3.107066 if elecdist == 265
replace enep = 2.740554 if elecdist == 266
replace enep = 2.880157 if elecdist == 267
replace enep = 2.934901 if elecdist == 268
replace enep = 2.643753 if elecdist == 269
replace enep = 2.482915 if elecdist == 270
replace enep = 3.722134 if elecdist == 271
replace enep = 2.770308 if elecdist == 272
replace enep = 2.851383 if elecdist == 273
replace enep = 3.551289 if elecdist == 274
replace enep = 3.509506 if elecdist == 275
replace enep = 2.485598 if elecdist == 276
replace enep = 3.068968 if elecdist == 277
replace enep = 2.945647 if elecdist == 278
replace enep = 3.205517 if elecdist == 279
replace enep = 2.55011 if elecdist == 280
replace enep = 3.852592 if elecdist == 281
replace enep = 3.028255 if elecdist == 282
replace enep = 2.779422 if elecdist == 283
replace enep = 2.709553 if elecdist == 284
replace enep = 2.648409 if elecdist == 285
replace enep = 2.71992 if elecdist == 286
replace enep = 3.042852 if elecdist == 287
replace enep = 2.807443 if elecdist == 288
replace enep = 3.024846 if elecdist == 289
replace enep = 3.384734 if elecdist == 290
replace enep = 2.925574 if elecdist == 291
replace enep = 2.508068 if elecdist == 292
replace enep = 2.85643 if elecdist == 293
replace enep = 3.041417 if elecdist == 294
replace enep = 2.429855 if elecdist == 295
replace enep = 3.50976 if elecdist == 296
replace enep = 3.038177 if elecdist == 297
replace enep = 3.047947 if elecdist == 298
replace enep = 3.201143 if elecdist == 299
   

*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if elecdist == 1
replace reelected = 1 if elecdist == 2
replace reelected = 1 if elecdist == 3
replace reelected = 1 if elecdist == 4
replace reelected = 1 if elecdist == 5
replace reelected = 1 if elecdist == 6
replace reelected = 1 if elecdist == 7
replace reelected = 1 if elecdist == 8
replace reelected = 1 if elecdist == 9
replace reelected = 1 if elecdist == 10
replace reelected = 1 if elecdist == 11
replace reelected = 1 if elecdist == 12
replace reelected = 1 if elecdist == 13
replace reelected = 0 if elecdist == 14
replace reelected = 1 if elecdist == 15
replace reelected = 1 if elecdist == 16
replace reelected = 1 if elecdist == 17
replace reelected = 1 if elecdist == 18
replace reelected = 1 if elecdist == 19
replace reelected = 0 if elecdist == 20
replace reelected = 1 if elecdist == 21
replace reelected = 0 if elecdist == 22
replace reelected = 1 if elecdist == 23
replace reelected = 1 if elecdist == 24
replace reelected = 1 if elecdist == 25
replace reelected = 1 if elecdist == 26
replace reelected = 0 if elecdist == 27
replace reelected = 1 if elecdist == 28
replace reelected = 1 if elecdist == 29
replace reelected = 1 if elecdist == 30
replace reelected = 1 if elecdist == 31
replace reelected = 1 if elecdist == 32
replace reelected = 1 if elecdist == 33
replace reelected = 1 if elecdist == 34
replace reelected = 1 if elecdist == 35
replace reelected = 1 if elecdist == 36
replace reelected = 1 if elecdist == 37
replace reelected = 1 if elecdist == 38
replace reelected = 1 if elecdist == 39
replace reelected = 1 if elecdist == 40
replace reelected = 1 if elecdist == 41
replace reelected = 1 if elecdist == 42
replace reelected = 0 if elecdist == 43
replace reelected = 1 if elecdist == 44
replace reelected = 1 if elecdist == 45
replace reelected = 1 if elecdist == 46
replace reelected = 1 if elecdist == 47
replace reelected = 0 if elecdist == 48
replace reelected = 1 if elecdist == 49
replace reelected = 1 if elecdist == 50
replace reelected = 1 if elecdist == 51
replace reelected = 1 if elecdist == 52
replace reelected = 1 if elecdist == 53
replace reelected = 1 if elecdist == 54
replace reelected = 1 if elecdist == 55
replace reelected = 0 if elecdist == 56
replace reelected = 0 if elecdist == 57
replace reelected = 0 if elecdist == 58
replace reelected = 0 if elecdist == 59
replace reelected = 1 if elecdist == 60
replace reelected = 0 if elecdist == 61
replace reelected = 0 if elecdist == 62
replace reelected = 0 if elecdist == 63
replace reelected = 0 if elecdist == 64
replace reelected = 1 if elecdist == 65
replace reelected = 0 if elecdist == 66
replace reelected = 1 if elecdist == 67
replace reelected = 1 if elecdist == 68
replace reelected = 0 if elecdist == 69
replace reelected = 1 if elecdist == 70
replace reelected = 0 if elecdist == 71
replace reelected = 0 if elecdist == 72
replace reelected = 1 if elecdist == 73
replace reelected = 0 if elecdist == 74
replace reelected = 1 if elecdist == 75
replace reelected = 1 if elecdist == 76
replace reelected = 1 if elecdist == 77
replace reelected = 1 if elecdist == 78
replace reelected = 1 if elecdist == 79
replace reelected = 0 if elecdist == 80
replace reelected = 1 if elecdist == 81
replace reelected = 0 if elecdist == 82
replace reelected = 1 if elecdist == 83
replace reelected = 1 if elecdist == 84
replace reelected = 1 if elecdist == 85
replace reelected = 1 if elecdist == 86
replace reelected = 1 if elecdist == 87
replace reelected = 1 if elecdist == 88
replace reelected = 1 if elecdist == 89
replace reelected = 1 if elecdist == 90
replace reelected = 1 if elecdist == 91
replace reelected = 1 if elecdist == 92
replace reelected = 1 if elecdist == 93
replace reelected = 1 if elecdist == 94
replace reelected = 1 if elecdist == 95
replace reelected = 1 if elecdist == 96
replace reelected = 1 if elecdist == 97
replace reelected = 1 if elecdist == 98
replace reelected = 1 if elecdist == 99
replace reelected = 1 if elecdist == 100
replace reelected = 1 if elecdist == 101
replace reelected = 1 if elecdist == 102
replace reelected = 1 if elecdist == 103
replace reelected = 1 if elecdist == 104
replace reelected = 1 if elecdist == 105
replace reelected = 1 if elecdist == 106
replace reelected = 1 if elecdist == 107
replace reelected = 1 if elecdist == 108
replace reelected = 1 if elecdist == 109
replace reelected = 1 if elecdist == 110
replace reelected = 1 if elecdist == 111
replace reelected = 1 if elecdist == 112
replace reelected = 1 if elecdist == 113
replace reelected = 1 if elecdist == 114
replace reelected = 1 if elecdist == 115
replace reelected = 1 if elecdist == 116
replace reelected = 1 if elecdist == 117
replace reelected = 1 if elecdist == 118
replace reelected = 1 if elecdist == 119
replace reelected = 0 if elecdist == 120
replace reelected = 1 if elecdist == 121
replace reelected = 1 if elecdist == 122
replace reelected = 1 if elecdist == 123
replace reelected = 1 if elecdist == 124
replace reelected = 1 if elecdist == 125
replace reelected = 1 if elecdist == 126
replace reelected = 1 if elecdist == 127
replace reelected = 1 if elecdist == 128
replace reelected = 1 if elecdist == 129
replace reelected = 1 if elecdist == 130
replace reelected = 1 if elecdist == 131
replace reelected = 0 if elecdist == 132
replace reelected = 1 if elecdist == 133
replace reelected = 1 if elecdist == 134
replace reelected = 1 if elecdist == 135
replace reelected = 1 if elecdist == 136
replace reelected = 1 if elecdist == 137
replace reelected = 1 if elecdist == 138
replace reelected = 1 if elecdist == 139
replace reelected = 1 if elecdist == 140
replace reelected = 1 if elecdist == 141
replace reelected = 1 if elecdist == 142
replace reelected = 1 if elecdist == 143
replace reelected = 1 if elecdist == 144
replace reelected = 1 if elecdist == 145
replace reelected = 1 if elecdist == 146
replace reelected = 1 if elecdist == 147
replace reelected = 1 if elecdist == 148
replace reelected = 1 if elecdist == 149
replace reelected = 1 if elecdist == 150
replace reelected = 1 if elecdist == 151
replace reelected = 1 if elecdist == 152
replace reelected = 1 if elecdist == 153
replace reelected = 1 if elecdist == 154
replace reelected = 1 if elecdist == 155
replace reelected = 1 if elecdist == 156
replace reelected = 1 if elecdist == 157
replace reelected = 1 if elecdist == 158
replace reelected = 1 if elecdist == 159
replace reelected = 1 if elecdist == 160
replace reelected = 1 if elecdist == 161
replace reelected = 1 if elecdist == 162
replace reelected = 1 if elecdist == 163
replace reelected = 1 if elecdist == 164
replace reelected = 1 if elecdist == 165
replace reelected = 1 if elecdist == 166
replace reelected = 0 if elecdist == 167
replace reelected = 1 if elecdist == 168
replace reelected = 1 if elecdist == 169
replace reelected = 1 if elecdist == 170
replace reelected = 1 if elecdist == 171
replace reelected = 1 if elecdist == 172
replace reelected = 1 if elecdist == 173
replace reelected = 1 if elecdist == 174
replace reelected = 1 if elecdist == 175
replace reelected = 1 if elecdist == 176
replace reelected = 1 if elecdist == 177
replace reelected = 1 if elecdist == 178
replace reelected = 1 if elecdist == 179
replace reelected = 1 if elecdist == 180
replace reelected = 1 if elecdist == 181
replace reelected = 1 if elecdist == 182
replace reelected = 1 if elecdist == 183
replace reelected = 1 if elecdist == 184
replace reelected = 1 if elecdist == 185
replace reelected = 1 if elecdist == 186
replace reelected = 1 if elecdist == 187
replace reelected = 1 if elecdist == 188
replace reelected = 1 if elecdist == 189
replace reelected = 1 if elecdist == 190
replace reelected = 1 if elecdist == 191
replace reelected = 1 if elecdist == 192
replace reelected = 1 if elecdist == 193
replace reelected = 0 if elecdist == 194
replace reelected = 1 if elecdist == 195
replace reelected = 1 if elecdist == 196
replace reelected = 0 if elecdist == 197
replace reelected = 1 if elecdist == 198
replace reelected = 1 if elecdist == 199
replace reelected = 1 if elecdist == 200
replace reelected = 1 if elecdist == 201
replace reelected = 1 if elecdist == 202
replace reelected = 1 if elecdist == 203
replace reelected = 1 if elecdist == 204
replace reelected = 1 if elecdist == 205
replace reelected = 1 if elecdist == 206
replace reelected = 0 if elecdist == 207
replace reelected = 1 if elecdist == 208
replace reelected = 1 if elecdist == 209
replace reelected = 1 if elecdist == 210
replace reelected = 1 if elecdist == 211
replace reelected = 1 if elecdist == 212
replace reelected = 1 if elecdist == 213
replace reelected = 1 if elecdist == 214
replace reelected = 1 if elecdist == 215
replace reelected = 1 if elecdist == 216
replace reelected = 1 if elecdist == 217
replace reelected = 1 if elecdist == 218
replace reelected = 1 if elecdist == 219
replace reelected = 1 if elecdist == 220
replace reelected = 1 if elecdist == 221
replace reelected = 1 if elecdist == 222
replace reelected = 1 if elecdist == 223
replace reelected = 1 if elecdist == 224
replace reelected = 1 if elecdist == 225
replace reelected = 1 if elecdist == 226
replace reelected = 1 if elecdist == 227
replace reelected = 1 if elecdist == 228
replace reelected = 1 if elecdist == 229
replace reelected = 1 if elecdist == 230
replace reelected = 1 if elecdist == 231
replace reelected = 1 if elecdist == 232
replace reelected = 1 if elecdist == 233
replace reelected = 1 if elecdist == 234
replace reelected = 1 if elecdist == 235
replace reelected = 1 if elecdist == 236
replace reelected = 1 if elecdist == 237
replace reelected = 1 if elecdist == 238
replace reelected = 1 if elecdist == 239
replace reelected = 1 if elecdist == 240
replace reelected = 1 if elecdist == 241
replace reelected = 1 if elecdist == 242
replace reelected = 1 if elecdist == 243
replace reelected = 1 if elecdist == 244
replace reelected = 1 if elecdist == 245
replace reelected = 1 if elecdist == 246
replace reelected = 1 if elecdist == 247
replace reelected = 1 if elecdist == 248
replace reelected = 1 if elecdist == 249
replace reelected = 1 if elecdist == 250
replace reelected = 1 if elecdist == 251
replace reelected = 1 if elecdist == 252
replace reelected = 1 if elecdist == 253
replace reelected = 1 if elecdist == 254
replace reelected = 1 if elecdist == 255
replace reelected = 1 if elecdist == 256
replace reelected = 1 if elecdist == 257
replace reelected = 1 if elecdist == 258
replace reelected = 1 if elecdist == 259
replace reelected = 1 if elecdist == 260
replace reelected = 1 if elecdist == 261
replace reelected = 1 if elecdist == 262
replace reelected = 1 if elecdist == 263
replace reelected = 1 if elecdist == 264
replace reelected = 1 if elecdist == 265
replace reelected = 1 if elecdist == 266
replace reelected = 1 if elecdist == 267
replace reelected = 1 if elecdist == 268
replace reelected = 1 if elecdist == 269
replace reelected = 1 if elecdist == 270
replace reelected = 1 if elecdist == 271
replace reelected = 1 if elecdist == 272
replace reelected = 1 if elecdist == 273
replace reelected = 1 if elecdist == 274
replace reelected = 1 if elecdist == 275
replace reelected = 1 if elecdist == 276
replace reelected = 1 if elecdist == 277
replace reelected = 1 if elecdist == 278
replace reelected = 1 if elecdist == 279
replace reelected = 1 if elecdist == 280
replace reelected = 0 if elecdist == 281
replace reelected = 1 if elecdist == 282
replace reelected = 1 if elecdist == 283
replace reelected = 1 if elecdist == 284
replace reelected = 1 if elecdist == 285
replace reelected = 1 if elecdist == 286
replace reelected = 1 if elecdist == 287
replace reelected = 1 if elecdist == 288
replace reelected = 1 if elecdist == 289
replace reelected = 1 if elecdist == 290
replace reelected = 1 if elecdist == 291
replace reelected = 1 if elecdist == 292
replace reelected = 1 if elecdist == 293
replace reelected = 1 if elecdist == 294
replace reelected = 1 if elecdist == 295
replace reelected = 1 if elecdist == 296
replace reelected = 1 if elecdist == 297
replace reelected = 1 if elecdist == 298
replace reelected = 1 if elecdist == 299


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen sdate = ""

replace sdate = "July 01, 2013" if datum == 70113
replace sdate = "July 02, 2013" if datum == 70213
replace sdate = "July 03, 2013" if datum == 70313
replace sdate = "July 04, 2013" if datum == 70413
replace sdate = "July 05, 2013" if datum == 70513
replace sdate = "July 06, 2013" if datum == 70613
replace sdate = "July 07, 2013" if datum == 70713
replace sdate = "July 08, 2013" if datum == 70813
replace sdate = "July 09, 2013" if datum == 70913
replace sdate = "July 10, 2013" if datum == 71013
replace sdate = "July 11, 2013" if datum == 71113
replace sdate = "July 12, 2013" if datum == 71213
replace sdate = "July 13, 2013" if datum == 71313
replace sdate = "July 14, 2013" if datum == 71413
replace sdate = "July 15, 2013" if datum == 71513
replace sdate = "July 16, 2013" if datum == 71613
replace sdate = "July 17, 2013" if datum == 71713
replace sdate = "July 18, 2013" if datum == 71813
replace sdate = "July 19, 2013" if datum == 71913
replace sdate = "July 20, 2013" if datum == 72013
replace sdate = "July 21, 2013" if datum == 72113
replace sdate = "July 22, 2013" if datum == 72213
replace sdate = "July 23, 2013" if datum == 72313
replace sdate = "July 24, 2013" if datum == 72413
replace sdate = "July 25, 2013" if datum == 72513
replace sdate = "July 26, 2013" if datum == 72613
replace sdate = "July 27, 2013" if datum == 72713
replace sdate = "July 28, 2013" if datum == 72813
replace sdate = "July 29, 2013" if datum == 72913
replace sdate = "July 30, 2013" if datum == 73013
replace sdate = "July 31, 2013" if datum == 73113
replace sdate = "August 01, 2013" if datum == 80113
replace sdate = "August 02, 2013" if datum == 80213
replace sdate = "August 03, 2013" if datum == 80313
replace sdate = "August 04, 2013" if datum == 80413
replace sdate = "August 05, 2013" if datum == 80513
replace sdate = "August 06, 2013" if datum == 80613
replace sdate = "August 07, 2013" if datum == 80713
replace sdate = "August 08, 2013" if datum == 80813
replace sdate = "August 09, 2013" if datum == 80913
replace sdate = "August 10, 2013" if datum == 81013
replace sdate = "August 11, 2013" if datum == 81113
replace sdate = "August 12, 2013" if datum == 81213
replace sdate = "August 13, 2013" if datum == 81313
replace sdate = "August 14, 2013" if datum == 81413
replace sdate = "August 15, 2013" if datum == 81513
replace sdate = "August 16, 2013" if datum == 81613
replace sdate = "August 17, 2013" if datum == 81713
replace sdate = "August 18, 2013" if datum == 81813
replace sdate = "August 19, 2013" if datum == 81913
replace sdate = "August 20, 2013" if datum == 82013
replace sdate = "August 21, 2013" if datum == 82113
replace sdate = "August 22, 2013" if datum == 82213
replace sdate = "August 23, 2013" if datum == 82313
replace sdate = "August 24, 2013" if datum == 82413
replace sdate = "August 25, 2013" if datum == 82513
replace sdate = "August 26, 2013" if datum == 82613
replace sdate = "August 27, 2013" if datum == 82713
replace sdate = "August 28, 2013" if datum == 82813
replace sdate = "August 29, 2013" if datum == 82913
replace sdate = "August 30, 2013" if datum == 83013
replace sdate = "August 31, 2013" if datum == 83113
replace sdate = "September 01, 2013" if datum == 90113
replace sdate = "September 02, 2013" if datum == 90213
replace sdate = "September 03, 2013" if datum == 90313
replace sdate = "September 04, 2013" if datum == 90413
replace sdate = "September 05, 2013" if datum == 90513
replace sdate = "September 06, 2013" if datum == 90613
replace sdate = "September 07, 2013" if datum == 90713
replace sdate = "September 08, 2013" if datum == 90813
replace sdate = "September 09, 2013" if datum == 90913
replace sdate = "September 10, 2013" if datum == 91013
replace sdate = "September 11, 2013" if datum == 91113
replace sdate = "September 12, 2013" if datum == 91213
replace sdate = "September 13, 2013" if datum == 91313
replace sdate = "September 14, 2013" if datum == 91413
replace sdate = "September 15, 2013" if datum == 91513
replace sdate = "September 16, 2013" if datum == 91613
replace sdate = "September 17, 2013" if datum == 91713
replace sdate = "September 18, 2013" if datum == 91813
replace sdate = "September 19, 2013" if datum == 91913
replace sdate = "September 20, 2013" if datum == 92013
replace sdate = "September 21, 2013" if datum == 92113
replace sdate = "September 22, 2013" if datum == 92213
replace sdate = "September 23, 2013" if datum == 92313
replace sdate = "September 24, 2013" if datum == 92413
replace sdate = "September 25, 2013" if datum == 92513
replace sdate = "September 26, 2013" if datum == 92613
replace sdate = "September 27, 2013" if datum == 92713
replace sdate = "September 28, 2013" if datum == 92813
replace sdate = "September 29, 2013" if datum == 92913
replace sdate = "September 30, 2013" if datum == 93013
replace sdate = "October 01, 2013" if datum == 100113
replace sdate = "October 02, 2013" if datum == 100213
replace sdate = "October 03, 2013" if datum == 100313
replace sdate = "October 04, 2013" if datum == 100413
replace sdate = "October 05, 2013" if datum == 100513
replace sdate = "October 06, 2013" if datum == 100613
replace sdate = "October 07, 2013" if datum == 100713
replace sdate = "October 08, 2013" if datum == 100813
replace sdate = "October 09, 2013" if datum == 100913
replace sdate = "October 10, 2013" if datum == 101013
replace sdate = "October 11, 2013" if datum == 101113
replace sdate = "October 12, 2013" if datum == 101213
replace sdate = "October 13, 2013" if datum == 101313
replace sdate = "October 14, 2013" if datum == 101413
replace sdate = "October 15, 2013" if datum == 101513
replace sdate = "October 16, 2013" if datum == 101613
replace sdate = "October 17, 2013" if datum == 101713
replace sdate = "October 18, 2013" if datum == 101813
replace sdate = "October 19, 2013" if datum == 101913
replace sdate = "October 20, 2013" if datum == 102013
replace sdate = "October 21, 2013" if datum == 102113
replace sdate = "October 22, 2013" if datum == 102213
replace sdate = "October 23, 2013" if datum == 102313
replace sdate = "October 24, 2013" if datum == 102413
replace sdate = "October 25, 2013" if datum == 102513
replace sdate = "October 26, 2013" if datum == 102613
replace sdate = "October 27, 2013" if datum == 102713
replace sdate = "October 28, 2013" if datum == 102813
replace sdate = "October 29, 2013" if datum == 102913
replace sdate = "October 30, 2013" if datum == 103013
replace sdate = "October 31, 2013" if datum == 103113
replace sdate = "November 01, 2013" if datum == 110113
replace sdate = "November 02, 2013" if datum == 110213
replace sdate = "November 03, 2013" if datum == 110313
replace sdate = "November 04, 2013" if datum == 110413
replace sdate = "November 05, 2013" if datum == 110513
replace sdate = "November 06, 2013" if datum == 110613
replace sdate = "November 07, 2013" if datum == 110713
replace sdate = "November 08, 2013" if datum == 110813
replace sdate = "November 09, 2013" if datum == 110913
replace sdate = "November 10, 2013" if datum == 111013
replace sdate = "November 11, 2013" if datum == 111113
replace sdate = "November 12, 2013" if datum == 111213
replace sdate = "November 13, 2013" if datum == 111313
replace sdate = "November 14, 2013" if datum == 111413
replace sdate = "November 15, 2013" if datum == 111513
replace sdate = "November 16, 2013" if datum == 111613
replace sdate = "November 17, 2013" if datum == 111713
replace sdate = "November 18, 2013" if datum == 111813
replace sdate = "November 19, 2013" if datum == 111913
replace sdate = "November 20, 2013" if datum == 112013
replace sdate = "November 21, 2013" if datum == 112113
replace sdate = "November 22, 2013" if datum == 112213
replace sdate = "November 23, 2013" if datum == 112313
replace sdate = "November 24, 2013" if datum == 112413
replace sdate = "November 25, 2013" if datum == 112513
replace sdate = "November 26, 2013" if datum == 112613
replace sdate = "November 27, 2013" if datum == 112713
replace sdate = "November 28, 2013" if datum == 112813
replace sdate = "November 29, 2013" if datum == 112913
replace sdate = "November 30, 2013" if datum == 113013
replace sdate = "December 01, 2013" if datum == 120113
replace sdate = "December 02, 2013" if datum == 120213
replace sdate = "December 03, 2013" if datum == 120313
replace sdate = "December 04, 2013" if datum == 120413
replace sdate = "December 05, 2013" if datum == 120513
replace sdate = "December 06, 2013" if datum == 120613
replace sdate = "December 07, 2013" if datum == 120713
replace sdate = "December 08, 2013" if datum == 120813
replace sdate = "December 09, 2013" if datum == 120913
replace sdate = "December 10, 2013" if datum == 121013
replace sdate = "December 11, 2013" if datum == 121113
replace sdate = "December 12, 2013" if datum == 121213
replace sdate = "December 13, 2013" if datum == 121313
replace sdate = "December 14, 2013" if datum == 121413
replace sdate = "December 15, 2013" if datum == 121513
replace sdate = "December 16, 2013" if datum == 121613
replace sdate = "December 17, 2013" if datum == 121713
replace sdate = "December 18, 2013" if datum == 121813
replace sdate = "December 19, 2013" if datum == 121913
replace sdate = "December 20, 2013" if datum == 122013
replace sdate = "December 21, 2013" if datum == 122113
replace sdate = "December 22, 2013" if datum == 122213
replace sdate = "December 23, 2013" if datum == 122313
replace sdate = "December 24, 2013" if datum == 122413
replace sdate = "December 25, 2013" if datum == 122513
replace sdate = "December 26, 2013" if datum == 122613
replace sdate = "December 27, 2013" if datum == 122713
replace sdate = "December 28, 2013" if datum == 122813
replace sdate = "December 29, 2013" if datum == 122913
replace sdate = "December 30, 2013" if datum == 123013
replace sdate = "December 31, 2013" if datum == 123113

generate surveydate = date(sdate, "MDY") // Interview date

format %tdMon_DD,_CCYY surveydate // Format interview date: DDMMYYYY

gen edate = 20130922 // Election date

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate // Format election date: DDMMYYYY

gen time = electiondate - surveydate // Number of days between interview and election

bysort elecdist: egen std_time = std(time) // Variation in response date by district

* Gender

gen male = vn1 // Gender

recode male (2=0) // Recode female = 0

* Age

gen age = 2013 - vn2c // Age (survey year - year of birth)

replace age = . if age < 17 // Recode respondents less than 17 years old as missing data

* Education

gen education = vn163 // Highest level of general education

recode education (-99=.) // Recode "still at school" as missing data
recode education (6=.) // Recode "don't know" as missing data
recode education (9=.) // Recode "no answer" as missing data

* Interest for election

gen interest = v4

recode interest (-99=.) // Recode "not aspplicable" as missing data
recode interest (-98=.) // Recode "don't know" as missing data
recode interest (-94=.) // Recode "no answer" as missing data

replace interest = 6 - interest // Interest for election (original scale inverted)

* Interest in the outcome

gen care_whole = v5

recode care_whole (-99=.) // Recode "not aspplicable" as missing data
recode care_whole (-98=.) // Recode "don't know" as missing data
recode care_whole (-94=.) // Recode "no answer" as missing data

replace care_whole = 6 - care_whole // Interest in the outcome (original scale inverted)

* Attention paid to election campaign

gen attention = n262

recode attention (-99=.) // Recode "not aspplicable" as missing data
recode attention (-98=.) // Recode "don't know" as missing data
recode attention (-94=.) // Recode "no answer" as missing data

replace attention = 6 - attention // Attention paid to election campaign (original scale inverted)

* Party identification (national) 

gen pid = vn119a

recode pid (2=1) // CDU recoded as CDU/CSU
recode pid (3=1) // CSU recoded as CDU/CSU

gen pidstatus_whole = .

replace pidstatus_whole = 9999 if vn119a == .
replace pidstatus_whole = 9999 if vn119a == -99
replace pidstatus_whole = 9999 if vn119a == -98

replace pidstatus_whole = 2 if vn119a == 808

replace pidstatus_whole = 3 if vn119a == 1 & pidstatus_whole!=2 & pidstatus_whole!=9999 | vn119a == 4 & pidstatus_whole!=2 & pidstatus_whole!=9999

replace pidstatus_whole = 1 if vn119a!=1 & vn119a!=. & vn119a!=4 & pidstatus_whole!=2 & pidstatus_whole!=3 & pidstatus_whole!=9999

replace pidstatus_whole = . if pidstatus_whole == 9999

label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID"
label values pidstatus_whole pidstatus

* Party identification scale (national)

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & vn120 == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & vn120 == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & vn120 == 3
replace pidscale_whole = 4 if pidstatus_whole == 1 & vn120 == 4
replace pidscale_whole = 5 if pidstatus_whole == 1 & vn120 == 5
replace pidscale_whole = 6 if pidstatus_whole == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & vn120 == 5
replace pidscale_whole = 8 if pidstatus_whole == 3 & vn120 == 4
replace pidscale_whole = 9 if pidstatus_whole == 3 & vn120 == 3
replace pidscale_whole = 10 if pidstatus_whole == 3 & vn120 == 2
replace pidscale_whole = 11 if pidstatus_whole == 3 & vn120 == 1

label define pidscale 1 "Very strong loser PID" 2 "Fairly strong loser PID" 3 "Moderate loser PID" 4 "Fairly weak loser PID" 5 "Very weak loser PID" 6 "No PID" 7 "Very weak winner PID" 8 "Fairly weak winner PID" 9 "Moderate winner PID" 10 "Fairly strong winner PID" 11 "Very strong winner PID"
label values pidscale_whole pidscale

* Party identification (district)  

gen pidstatus_district = .

replace pidstatus_district = . if pid == -98 // Recode "don't know" as missing data
replace pidstatus_district = . if pid == -99 // Recode no answer as missing data

replace pidstatus_district = 2 if pid == 808 // No party

replace pidstatus_district = 3 if pid == district_outcome & pidstatus_district!=2 // Winner PID

replace pidstatus_district = 1 if pid!=district_outcome & pid!=. & pidstatus_district!=2 & pidstatus_district!=3 // Loser PID

label values pidstatus_district pidstatus

* Party identification scale

gen pidscale_district = .

replace pidscale_district = 1 if pidstatus_district == 1 & vn120 == 1 // Very strong loser PID
replace pidscale_district = 2 if pidstatus_district == 1 & vn120 == 2 // Fairly strong loser PID
replace pidscale_district = 3 if pidstatus_district == 1 & vn120 == 3 // Moderate loser PID
replace pidscale_district = 4 if pidstatus_district == 1 & vn120 == 4 // Fairly weak loser PID
replace pidscale_district = 5 if pidstatus_district == 1 & vn120 == 5 // Very weak loser PID
replace pidscale_district = 6 if pidstatus_district == 2 // No PID
replace pidscale_district = 7 if pidstatus_district == 3 & vn120 == 5 // Very weak winner PID
replace pidscale_district = 8 if pidstatus_district == 3 & vn120 == 4 // Fairly weak winner PID
replace pidscale_district = 9 if pidstatus_district == 3 & vn120 == 3 // Moderate winner PID
replace pidscale_district = 10 if pidstatus_district == 3 & vn120 == 2 // Fairly strong winner PID
replace pidscale_district = 11 if pidstatus_district == 3 & vn120 == 1 // Very strong winner PID

label values pidscale_district pidscale

* Coalition preference

gen coalprefA = v79a
gen coalprefB = v79b
gen coalprefC = v79c
gen coalprefD = v79d
gen coalprefE = v79e
gen coalprefF = v79f

recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF (-94=.)
recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF (-98=.)
recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF (-99=.)

gen coalpref_complete = 1 if coalprefA!=. & coalprefB!=. & coalprefC!=. & coalprefD!=. & coalprefE!=. & coalprefF!=. 

gen coalpref = 3 if coalprefC >= coalprefA & coalprefC >= coalprefB & coalprefC >= coalprefD & coalprefC >= coalprefE & coalprefC >= coalprefF & coalpref_complete == 1
replace coalpref = 2 if coalprefA == coalprefB & coalprefA == coalprefC & coalprefA == coalprefD & coalprefA == coalprefE & coalprefA == coalprefF & coalpref_complete == 1
replace coalpref = 1 if coalpref!=2 & coalpref!=3 & coalpref_complete == 1

replace coalpref = . if coalpref == 3 & coalprefC == coalprefA & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefB & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefD & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefE & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefF & coalpref_complete == 1

label define coalpref 1 "Loser coalition" 2 "No preference" 3 "Winner coalition"
label values coalpref coalpref

* Coalition preference strength

gen coalstrengthA = coalprefA if coalprefA >= coalprefB & coalprefA >= coalprefC & coalprefA >= coalprefD & coalprefA >= coalprefE & coalprefA >= coalprefF & coalpref_complete == 1
gen coalstrengthB = coalprefB if coalprefB >= coalprefA & coalprefB >= coalprefC & coalprefB >= coalprefD & coalprefB >= coalprefE & coalprefB >= coalprefF & coalpref_complete == 1
gen coalstrengthC = coalprefC if coalprefC >= coalprefA & coalprefC >= coalprefB & coalprefC >= coalprefD & coalprefC >= coalprefE & coalprefC >= coalprefF & coalpref_complete == 1
gen coalstrengthD = coalprefD if coalprefD >= coalprefA & coalprefD >= coalprefB & coalprefD >= coalprefC & coalprefD >= coalprefE & coalprefD >= coalprefF & coalpref_complete == 1
gen coalstrengthE = coalprefE if coalprefE >= coalprefA & coalprefE >= coalprefB & coalprefE >= coalprefC & coalprefE >= coalprefD & coalprefE >= coalprefF & coalpref_complete == 1
gen coalstrengthF = coalprefF if coalprefF >= coalprefA & coalprefF >= coalprefB & coalprefF >= coalprefC & coalprefF >= coalprefD & coalprefF >= coalprefE & coalpref_complete == 1

gen coalstrength = max(coalstrengthA,coalstrengthB,coalstrengthC,coalstrengthD,coalstrengthE,coalstrengthF)

replace coalstrength = 300 if coalstrength == 8 | coalstrength == 9 | coalstrength == 10 | coalstrength == 11
replace coalstrength = 200 if coalstrength == 5 | coalstrength == 6 | coalstrength == 7
replace coalstrength = 100 if coalstrength == 1 | coalstrength == 2 | coalstrength == 3 | coalstrength == 4

replace coalstrength = 3 if coalstrength == 300
replace coalstrength = 2 if coalstrength == 200
replace coalstrength = 1 if coalstrength == 100

label define coalstrength 1 "Weak preference" 2 "Moderate preference" 3 "Strong preference"
label values coalstrength coalstrength

* Coalition preference scale

gen coalscale = .

replace coalscale = 1 if coalpref == 1 & coalstrength == 3
replace coalscale = 2 if coalpref == 1 & coalstrength == 2
replace coalscale = 3 if coalpref == 1 & coalstrength == 1
replace coalscale = 4 if coalpref == 2
replace coalscale = 5 if coalpref == 3 & coalstrength == 1
replace coalscale = 6 if coalpref == 3 & coalstrength == 2
replace coalscale = 7 if coalpref == 3 & coalstrength == 3

label define coalscale 1 "Strong loser preference" 2 "Moderate loser preference" 3 "Weak loser preference" 4 "No preference" 5 "Weak winner preference" 6 "Moderate winner preference" 7 "Strong winner preference" 
label values coalscale coalscale

* First/second vote and seats in parliament - Correct answer (second vote) = 1, Incorrect answer = 0

gen votes = vn7 // First/second vote and seats in parliament

recode votes (-94=.) // Recode "not in sampling frame" as missing data
recode votes (-99=.) // Recode "no answer" as missing data

replace votes = 0 if votes!=2 & votes!=. // Incorrect answer
replace votes = 1 if votes == 2 & votes!=. // Correct answer

* 5 percent threshold - Correct answer (5 percent) = 1, Incorrect answer = 0

gen threshold = vn15a // 5 percent threshold

* Picture of SPD leader - Correct answer (Peer Steinbrück) = 1, Incorrect answer = 0

gen spdleader = v155a // SPD leader

recode spdleader (-94=.) // Recode "not in sampling frame" as missing data
recode spdleader (-99=.) // Recode "no answer" as missing data

replace spdleader = 0 if spdleader!=4 & spdleader!=. // Incorrect answer
replace spdleader = 1 if spdleader == 4 & spdleader!=. // Correct answer

* Picture of FDP leader - Correct answer (Rainer Brüderle) = 1, Incorrect answer = 0

gen fdpleader = v155b // FDP leader

recode fdpleader (-94=.) // Recode "not in sampling frame" as missing data
recode fdpleader (-99=.) // Recode "no answer" as missing data

replace fdpleader = 0 if fdpleader!=5 & fdpleader!=. // Incorrect answer
replace fdpleader = 1 if fdpleader == 5 & fdpleader!=. // Correct answer

* Picture of Green leader - Correct answer (Katrin Göring-Eckardt) = 1, Incorrect answer = 0

gen gruneleader = v155c // Green leader

recode gruneleader (-94=.) // Recode "not in sampling frame" as missing data
recode gruneleader (-99=.) // Recode "no answer" as missing data

replace gruneleader = 0 if gruneleader!=6 & gruneleader!=. // Incorrect answer
replace gruneleader = 1 if gruneleader == 6 & gruneleader!=. // Correct answer

* Political knowledge index

gen knowledge = votes + threshold + spdleader + fdpleader + gruneleader // Political knowledge index

* Political discussion

gen discussion = v135

recode discussion (-94=.)
recode discussion (-98=.)
recode discussion (-99=.)

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.

* Network size

gen size = v136

recode size (-71=.)
recode size (-94=.)
recode size (-97=.)
recode size (-98=.)
recode size (-99=.)

replace size = 0 if discussion == 0

gen size_3pts = size
replace size_3pts = 0 if size == 1
replace size_3pts = 1 if size == 2
replace size_3pts = 1 if size == 3
replace size_3pts = 1 if size == 4
replace size_3pts = 2 if size > 4 & size!=.

* Network expertise

gen expertise1 = v140
gen expertise2 = v146

recode expertise1 expertise2 (-94=.)
recode expertise1 expertise2 (-97=.)
recode expertise1 expertise2 (-98=.)
recode expertise1 expertise2 (-99=.)

replace expertise1 = 4 - expertise1
replace expertise2 = 4 - expertise2

replace expertise1 = 0 if size == 0
replace expertise2 = 0 if size < 2

gen expertise = expertise1 + expertise2

egen expertise_4pts = rowmax(expertise1 expertise2)

* Political disagreement

gen disagreement1 = v141
gen disagreement2 = v147

recode disagreement1 disagreement2 (-94=.)
recode disagreement1 disagreement2 (-97=.)
recode disagreement1 disagreement2 (-98=.)
recode disagreement1 disagreement2 (-99=.)

replace disagreement1 = 4 - disagreement1
replace disagreement2 = 4 - disagreement2

replace disagreement1 = 0 if size == 0
replace disagreement2 = 0 if size < 2

gen disagreement = disagreement1 + disagreement2

gen disagreement1_3pts = disagreement1
gen disagreement2_3pts = disagreement2

replace disagreement1_3pts = 0 if disagreement1 == 1
replace disagreement1_3pts = 1 if disagreement1 == 2
replace disagreement1_3pts = 2 if disagreement1 == 3

replace disagreement2_3pts = 0 if disagreement2 == 1
replace disagreement2_3pts = 1 if disagreement2 == 2
replace disagreement2_3pts = 2 if disagreement2 == 3

egen disagreement_3pts = rowmax(disagreement1_3pts disagreement2_3pts)

* Main source of information

gen network_main = 1 if vn97 == 5

replace network_main = 0 if vn97!=5
replace network_main = . if vn97 == -98
replace network_main = . if vn97 == -99


*********************************
** NATIONAL-LEVEL EXPECTATIONS ** 
*********************************

* Generate variable identifying incomplete answers

gen winCoalitionA = v80a
gen winCoalitionB = v80b
gen winCoalitionC = v80c
gen winCoalitionD = v80d
gen winCoalitionE = v80e
gen winCoalitionF = v80f

recode winCoalitionA winCoalitionB winCoalitionC winCoalitionD winCoalitionE winCoalitionF (-99=.)
recode winCoalitionA winCoalitionB winCoalitionC winCoalitionD winCoalitionE winCoalitionF (-98=.)
recode winCoalitionA winCoalitionB winCoalitionC winCoalitionD winCoalitionE winCoalitionF (-94=.)

gen complete_whole = 1 if winCoalitionA!=. & winCoalitionB!=. & winCoalitionC!=. & winCoalitionD!=. & winCoalitionE!=. & winCoalitionF!=.

* Generate value of 1 if the expectation for one coalition is higher or equal to the expectations for at least one other coalition

gen coalitionA = 1 if winCoalitionA >= winCoalitionB & winCoalitionA >= winCoalitionC & winCoalitionA >= winCoalitionD & winCoalitionA >= winCoalitionE & winCoalitionA >= winCoalitionF & complete_whole == 1

gen coalitionB = 1 if winCoalitionB >= winCoalitionA & winCoalitionB >= winCoalitionC & winCoalitionB >= winCoalitionD & winCoalitionB >= winCoalitionE & winCoalitionB >= winCoalitionF & complete_whole == 1

gen coalitionC = 1 if winCoalitionC >= winCoalitionA & winCoalitionC >= winCoalitionB & winCoalitionC >= winCoalitionD & winCoalitionC >= winCoalitionE & winCoalitionC >= winCoalitionF & complete_whole == 1

gen coalitionD = 1 if winCoalitionD >= winCoalitionA & winCoalitionD >= winCoalitionB & winCoalitionD >= winCoalitionC & winCoalitionD >= winCoalitionE & winCoalitionD >= winCoalitionF & complete_whole == 1

gen coalitionE = 1 if winCoalitionE >= winCoalitionA & winCoalitionE >= winCoalitionB & winCoalitionE >= winCoalitionC & winCoalitionE >= winCoalitionD & winCoalitionE >= winCoalitionF & complete_whole == 1

gen coalitionF = 1 if winCoalitionF >= winCoalitionA & winCoalitionF >= winCoalitionB & winCoalitionF >= winCoalitionC & winCoalitionF >= winCoalitionD & winCoalitionF >= winCoalitionE & complete_whole == 1

gen coalitionOTH = 1 if winCoalitionA == 1 & winCoalitionB == 1 & winCoalitionC == 1 & winCoalitionD == 1 & winCoalitionE == 1 & winCoalitionF == 1 & complete_whole == 1

replace coalitionA = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionB = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionC = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionD = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionE = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionF = . if coalitionOTH == 1 & complete_whole == 1

* Generate variable for ambiguous forecasts

gen ambiguous_whole = .

replace ambiguous_whole = 99 if coalitionA == coalitionB & coalitionA!=. | coalitionA == coalitionC & coalitionA!=. | coalitionA == coalitionD & coalitionA!=. | coalitionA == coalitionE & coalitionA!=. | coalitionA == coalitionF & coalitionA!=.

replace ambiguous_whole = 99 if coalitionB == coalitionA & coalitionB!=. | coalitionB == coalitionC & coalitionB!=. | coalitionB == coalitionD & coalitionB!=. | coalitionB == coalitionE & coalitionB!=. | coalitionB == coalitionF & coalitionB!=.

replace ambiguous_whole = 99 if coalitionC == coalitionA & coalitionC!=. | coalitionC == coalitionB & coalitionC!=. | coalitionC == coalitionD & coalitionC!=. | coalitionC == coalitionE & coalitionC!=. | coalitionC == coalitionF & coalitionC!=.

replace ambiguous_whole = 99 if coalitionD == coalitionA & coalitionD!=. | coalitionD == coalitionB & coalitionD!=. | coalitionD == coalitionC & coalitionD!=. | coalitionD == coalitionE & coalitionD!=. | coalitionD == coalitionF & coalitionD!=.

replace ambiguous_whole = 99 if coalitionE == coalitionA & coalitionE!=. | coalitionE == coalitionB & coalitionE!=. | coalitionE == coalitionC & coalitionE!=. | coalitionE == coalitionD & coalitionE!=. | coalitionE == coalitionF & coalitionE!=.

replace ambiguous_whole = 99 if coalitionF == coalitionA & coalitionF!=. | coalitionF == coalitionB & coalitionF!=. | coalitionF == coalitionC & coalitionF!=. | coalitionF == coalitionD & coalitionF!=. | coalitionF == coalitionE & coalitionF!=.

* Generate value of 0 for lower expectations

replace coalitionA = 0 if coalitionA!=1 & coalitionA!=. & complete_whole == 1
replace coalitionB = 0 if coalitionB!=1 & coalitionB!=. & complete_whole == 1
replace coalitionC = 0 if coalitionC!=1 & coalitionC!=. & complete_whole == 1
replace coalitionD = 0 if coalitionD!=1 & coalitionD!=. & complete_whole == 1
replace coalitionE = 0 if coalitionE!=1 & coalitionE!=. & complete_whole == 1
replace coalitionF = 0 if coalitionF!=1 & coalitionF!=. & complete_whole == 1
replace coalitionOTH = 0 if coalitionOTH!=1 & coalitionOTH!=. & complete_whole == 1

* Forecasts (1 = CDU/CSU-FDP, 2 = SPD-Green, 3 = CDU/CSU-SPD, 4 = CDU/CSU-Green, 5 = SPD-FDP-Green, 6 = SPD-Green-Die Linke, 88 = other, 99 = ambiguous)

gen forecast_whole = .

replace forecast_whole = 1 if coalitionA == 1 & complete_whole == 1
replace forecast_whole = 2 if coalitionB == 1 & complete_whole == 1
replace forecast_whole = 3 if coalitionC == 1 & complete_whole == 1
replace forecast_whole = 4 if coalitionD == 1 & complete_whole == 1
replace forecast_whole = 5 if coalitionE == 1 & complete_whole == 1
replace forecast_whole = 6 if coalitionF == 1 & complete_whole == 1
replace forecast_whole = 88 if coalitionOTH == 1 & complete_whole == 1
replace forecast_whole = 99 if ambiguous_whole == 1 & complete_whole == 1

* Identify correct and ambiguous forecasts

gen correct_whole = 0 if forecast_whole!=3 & forecast_whole!=. & complete_whole == 1
replace correct_whole = 1 if forecast_whole == 3 & complete_whole == 1

replace correct_whole = 0 if v80a == -98 & v80b == -98 & v80c == -98 & v80d == -98 & v80e == -98 & v80f == -98

replace correct_whole = 99 if forecast_whole == 99 & coalitionC == 1 & complete_whole == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen correct_whole_d = correct_whole

replace correct_whole_d = 0 if correct_whole_d == 99
replace correct_whole_d = 1 if correct_whole_d == 1


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Forecasts (1 = CDU/CSU, 4 = SPD, 5 = FDP, 6 = Green, 7 = Die Linke)

gen forecast_district = v81a

recode forecast_district (-94=.) // Recode "not in sampling frame" as missing data
recode forecast_district (-99=.) // Recode "no answer" as missing data

* Identify correct forecasts

gen correct_district_d = 0 if forecast_district!=district_outcome & forecast_district!=. // The forecast is incorrect

replace correct_district_d = 1 if forecast_district == district_outcome & forecast_district!=. // The forecast is correct


**********
** SAVE **
**********

save "de2013_gles.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion disagreement expertise size coalscale interest care_whole knowledge age i.male education time  
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1

* Regression analysis and percentage of correct and incorrect forecasts (district)

melogit correct_district_d discussion disagreement expertise size pidscale_district interest knowledge age i.male education time enep || elecdist: , or
estimates table, star(.05 .01 .001)

tab correct_district_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Coalition formed (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable expertise "Network expertise"
label variable size "Network size"
label variable pidscale_whole "National PID"
label variable pidscale_district "District PID"
label variable coalscale "Coalition preference"
label variable interest "Interest"
label variable care_whole "Caring about outcome"
label variable knowledge "Political knowledge"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"

quietly logistic correct_whole_d discussion disagreement expertise size coalscale interest care_whole knowledge age i.male education time

estpost tabstat correct_whole_d discussion disagreement expertise size coalscale interest care_whole knowledge age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "de2013_gles_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2013 German federal election (GLES), national level") replace

eststo clear

quietly melogit correct_district_d discussion disagreement expertise size pidscale_district interest knowledge age i.male education time enep i.reelected || elecdist: , or

estpost tabstat correct_district_d discussion disagreement expertise size pidscale_district interest knowledge age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "de2013_gles_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2013 German federal election (GLES), district level") replace

eststo clear


*************************
** ADDITIONAL ANALYSES **
*************************

* Regression analysis (national)

logistic correct_whole_d i.network_main discussion disagreement expertise size coalscale interest care_whole knowledge age i.male education time  
estimates table, star(.05 .01 .001)

* Regression analysis (district)

melogit correct_district_d i.network_main discussion disagreement expertise size pidscale_district interest knowledge age i.male education time enep i.reelected || elecdist: , or
estimates table, star(.05 .01 .001)

log close